package com.vote.dao;

import java.util.List;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.SQLParam;

import com.vote.domain.VoteQuestion;

@DAO(catalog = "vote")
public interface VoteQuestionDAO {
	
	public static String INSERT_FIELD="question_title,question_type,questionnaire_id";
	
	public static String SELECT_FIELD=INSERT_FIELD+",question_count,question_id";
	
	public static String TABLE="vote_question";
	
	/**
	 * 根据问卷id获取该问卷的所有问题信息
	 * @param id
	 * @return
	 */
	@SQL("select "+SELECT_FIELD+" from " +TABLE+" where questionnaire_id=:id")
	  public List<VoteQuestion> getQuestionListByQuestionnaireId(@SQLParam("id") int id);
	
	/**
	 * 根据问题id获得该问题信息
	 * @param id
	 * @return
	 */
	@SQL("select "+SELECT_FIELD+" from " +TABLE+" where questionn_id=:id")
	  public VoteQuestion getQuestion(@SQLParam("id") int id);
	
	/**
	 * 插入问题
	 * @param question
	 */
	@SQL("insert into "+TABLE+"("+INSERT_FIELD+") values(:model.questionTitle,:model.questionType,:model.questionnaireId)")
	   public void insertQuestion(@SQLParam("model") VoteQuestion question);
	
	/**
	 * 根据id删除问题
	 * @param id
	 */
	@SQL("delete from "+TABLE+" where question_id=:id")
	   public void deleteQuestionById(@SQLParam("id") int id);
	
	/**
	 * 获取刚插入question的ID
	 * @return
	 */
	@SQL("select LAST_INSERT_ID()")
	public int getLastInsertId();
	
	
	/**
	 * 更新问卷count。如果该问题被回答，count加1
	 * @param id
	 */
	@SQL("update "+TABLE+" set question_count=question_count+1 where question_id=:id")
	public void addCount(@SQLParam("id") int id);
	

}
